package br.com.felinelayer.sample;

import br.com.felinelayer.common.Message;
import br.com.felinelayer.server.application.*;
import java.text.DecimalFormat;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 */
public class Sample extends Application {

    private FrameServer f;
    private ButtonServer btGravar;
    private ButtonServer btClose;
    private EditServer edEnter;
    private EditServer edExit;
    private EditServer edUnitario;
    private EditServer edQuant;
    private EditServer edTotal;

    /**
     *
     */
    private IEventListenerServer calculateOnExit =
            new IEventListenerServer() {
                @Override
                public void onEvent(ComponentServer sender, String eventName, Message message) {
                    calcular();
                }
            };
    /**
     *
     */
    private IEventListenerServer btGravarOnClick = new IEventListenerServer() {
        @Override
        public void onEvent(ComponentServer sender, String eventName, Message message) {
            addFrame(new SecondFrame()).show();
        }
    };
    /**
     *
     */
    private IEventListenerServer btFecharOnClick = new IEventListenerServer() {
        @Override
        public void onEvent(ComponentServer sender, String eventName, Message message) {
            sender.destroy();
        }
    };
    /**
     *
     */
    private IEventListenerServerCanDo fBeforeClose = new IEventListenerServerCanDo() {
        @Override
        public boolean onEvent(ComponentServer sender, String eventName, Message message) {
            // If is before close frame event
            showMessage("EVENT_BEFORE_CLOSE sender.getText:" + sender.getText());
            return showInput("Can close? (\"yes\")").equalsIgnoreCase("yes");
        }
    };

    private void calcular() {
        String sq = edQuant.getText();
        sq = sq.replace(".", "");
        sq = sq.replace(",", ".");
        //
        float q = Float.parseFloat(sq);
        String su = edUnitario.getText();
        su = su.replace(".", "");
        su = su.replace(",", ".");
        //
        float u = Float.parseFloat(su);
        float t = q * u;
        edTotal.setText(new DecimalFormat("###,###,##0.00").format(t));
    }

    @Override
    protected void doStart() {
        stopAuto = false;
        // Pergunta nome do usuário
        String nome = showInput("What's your name?");
        showMessage("Welcome " + nome + "!");
        // 
        f = addFrame().setWidth(700).setHeight(380);
        // 
        f.addLabel().setCol(1).setText("Client");
        f.addEdit().setCol(4).setText(nome).setWidth(100);
        // 
        edEnter = f.addEdit().setCol(11).addLabelLeft("Enter id");
        edExit = f.addEdit().setCol(17).addLabelLeft("Exit id");
        f.addRow();
        f.addRow();
        f.addEdit().setCol(1).setText("000001").setWidth(80)
                .addLabelTop("Code");
        f.addEdit().setCol(6).setText("MacBook Air").setWidth(100)
                .addLabelTop("Description");
        edUnitario = f.addEdit().setCol(11).setText("2.999,99").setWidth(80)
                .addLabelTop("Value");
        // 
        f.addRow();
        f.addRow();
        edQuant = f.addEdit().setCol(6).setText("2").setWidth(100)
                .addLabelTop("Quantity");
        edTotal = f.addEdit().setCol(11).setText("").setWidth(80)
                .addLabelTop("Total");
        // 
        f.addRow();
        btGravar = f.addButton().setCol(1).setText("Save").setWidth(80)
                .addOnClickListener(btGravarOnClick);
        // 
        f.addRow();
        btClose = f.addButton().setCol(1).setText("Close").setWidth(80)
                .addOnClickListener(btFecharOnClick);
        // 
        edQuant.addOnExitListener(calculateOnExit);
        edUnitario.addOnExitListener(calculateOnExit);
        edTotal.addOnExitListener(calculateOnExit);
        // 
        f.addRow();
        GridServer g = f.addGrid().setCol(1).setWidth(500).setHeight(100);
        GridColumnServer[] cs = GridColumnServer.addInt("Code").addStr("Description").
                addInt("Qtd").addInt("Value").make();
        g.setColumns(cs);
        
        g.addRow( "1", "PSP", "1", "1000" );
        g.addRow( "2", "Wii", "1", "1000" );
        g.addRow( "3", "XBOX", "1", "1000" );
        // 
        f.addOnEnterListener(new IEventListenerServer() {
            @Override
            public void onEvent(ComponentServer sender, String eventName, Message message) {
                edEnter.setText(sender.getId() + "");
            }
        });
        // 
        f.addOnExitListener(new IEventListenerServer() {
            @Override
            public void onEvent(ComponentServer sender, String eventName, Message message) {
                edExit.setText(sender.getId() + "");
            }
        });
        // 
        f.addOnBeforeCloseListener(fBeforeClose);
        f.show();
    }

    public Sample() {
        setApplicationName("Sample");
    }
}
